package com.daxt.utils.file;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.Format.TextMode;
import org.jdom2.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


import java.util.List;

import org.jdom2.CDATA;
import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.DocType;
import org.jdom2.EntityRef;
import org.jdom2.JDOMException;
import org.jdom2.ProcessingInstruction;
import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;

/**
 * @description: xml工具类
 *
 * @author: lig
 * @date: 2021/11/9
 */
public class JDomUtil {


    /**
     * @description: 创建xml
     *
     * @author: lig
     * @date: 2021-11-09
     */
    public static void createJDOM(File file) {
        try {
            // 创建一个根节点
            Element rootElement = new Element("root");
            Document doc = new Document(rootElement);

            // 在根节点下创建第一个子节点
            Element rootOneElement = new Element("person");
            rootOneElement.setAttribute(new Attribute("attr","root one"));

            // 在第一个子节点下创建第一个子节点
            Element childOneElement = new Element("people");
            childOneElement.setAttribute(new Attribute("attr","child one"));
            childOneElement.setText("person child one");

            // 在第一个子节点下创建第二个子节点
            Element childTwoElement = new Element("people");
            childTwoElement.setAttribute(new Attribute("attr","child two"));
            childTwoElement.setText("person child two");

            // 在根节点下创建第二个子节点
            Element rootTwoElement = new Element("person");
            rootTwoElement.setAttribute(new Attribute("attr","root two"));

            // 在第一个子节点下创建第一个子节点
            Element oneChildOneElement = new Element("people");
            oneChildOneElement.setAttribute(new Attribute("attr","child one"));
            oneChildOneElement.setText("person child one");

            // 在第一个子节点下创建第二个子节点
            Element twoChildTwoElement = new Element("people");
            twoChildTwoElement.setAttribute(new Attribute("attr","child two"));
            twoChildTwoElement.setText("person child two");

            rootOneElement.addContent(childOneElement);
            rootOneElement.addContent(childTwoElement);

            rootTwoElement.addContent(oneChildOneElement);
            rootTwoElement.addContent(twoChildTwoElement);

            doc.getRootElement().addContent(rootOneElement);
            doc.getRootElement().addContent(rootTwoElement);

            // 创建xml输出流操作类
            XMLOutputter xmlOutput = new XMLOutputter();

            // 设置xml格式化的属性
            Format f = Format.getRawFormat();
            f.setIndent("  "); // 文本缩进
            f.setTextMode(TextMode.TRIM_FULL_WHITE);
            f.setEncoding("utf-8");//设置编码
            xmlOutput.setFormat(f);

            // 把xml文件输出到指定的位置
            xmlOutput.output(doc, new FileOutputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * @description: 解析XML
     *
     * @author: lig
     * @date: 2021-11-09
     */
    public static void parseJDOM(File file) {
        try {
            // 创建一个sax解析器
            SAXBuilder builder = new SAXBuilder();

            // 根据xml结构转换成一个Document对象
            Document doc = builder.build(file);

            // 打印xml信息
            printXNL(doc.getContent());
        } catch(JDOMException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }




    private static void printXNL(List<Content> list) {
        for (Content temp : list) {
            if (temp instanceof Comment) { //获取的内容是注释
                Comment com = (Comment)temp;
                System.out.println("<--"+com.getText()+"-->");
            } else if (temp instanceof Element) { //获取的内容是元素
                Element elt = (Element) temp;
                List<Attribute> attrs = elt.getAttributes();
                System.out.print("<"+elt.getName()+"");
                for (Attribute t : attrs) {
                    System.out.print(" " + t.getName()+"=\""+t.getValue()+"\"");
                }
                System.out.print(">");
                printXNL(elt.getContent());
                System.out.print("</"+elt.getName()+">");
            } else if (temp instanceof ProcessingInstruction) { // 获取的内容是处理指令
                ProcessingInstruction pi = (ProcessingInstruction)temp;
                System.out.println("<?"+pi.getTarget()+""+pi.getData()+"?>");
            } else  if (temp instanceof EntityRef) {
                EntityRef ref = (EntityRef)temp;
                System.out.println("<--"+ref.getName()+"-->");
            } else if (temp instanceof Text) { //获取的内容是文本
                Text text = (Text)temp;
                if (!text.getText().trim().equals("")) {
                    System.out.print(text.getText());
                } else {
                    System.out.println();
                }
            } else if (temp instanceof CDATA) { // 获取的内容是CDATA
                CDATA cdata = (CDATA)temp;
                System.out.println("<![CDATA["+cdata.getText()+"]]>");
            } else  if (temp instanceof DocType) {
                DocType docType = (DocType)temp;
                System.out.println("<--"+docType.getCType()+"-->");
            }
        }
    }



    public static void main(String[] args) {
        String packPath = "F:\\zjq\\33000019-01-00190-003-2017-5年-0035032\\归档配置范围.xml";
        File f = new File(packPath);
        if(!f.isDirectory())f.mkdir();
        //createJDOM(new File(packPath + "/aaa.xml"));
        parseJDOM(f);

    }
}
